<?php
/**
 * Clase Base DP para BaseSoDeliveryDP
 *
 * @package    
 * @subpackage BaseSoDeliveryDP
 * @author     hans
 * @version    SVN:: BaseSoDeliveryDP.class.php,v 1.1 2012-05-01 22:33:43 Hans.parra Exp $
 */

require_once('class/common/arrayClass.class.php');
require_once('class/im/dlv/BN/SoDeliveryBN.class.php');

abstract class BaseSoDeliveryDP extends SoDeliveryBN
{
  /* N�mero de registros afectados */
  private $nRowsAffected;

  public function __construct($bSetDefault = true)
  {
    if( $bSetDefault instanceof SoDeliveryBN )
    {
      $o_SoDeliveryBN = $bSetDefault;
      if($o_SoDeliveryBN->getId()) : $this->setId($o_SoDeliveryBN->getId()); endif;
      if($o_SoDeliveryBN->getTipo()) : $this->setTipo($o_SoDeliveryBN->getTipo()); endif;
      if($o_SoDeliveryBN->getDlvNum()) : $this->setDlvNum($o_SoDeliveryBN->getDlvNum()); endif;
      if($o_SoDeliveryBN->getAlmId()) : $this->setAlmId($o_SoDeliveryBN->getAlmId()); endif;
      if($o_SoDeliveryBN->getClienteId()) : $this->setClienteId($o_SoDeliveryBN->getClienteId()); endif;
      if($o_SoDeliveryBN->getClienteNombre()) : $this->setClienteNombre($o_SoDeliveryBN->getClienteNombre()); endif;
      if($o_SoDeliveryBN->getContacto()) : $this->setContacto($o_SoDeliveryBN->getContacto()); endif;
      if($o_SoDeliveryBN->getDocReferNum()) : $this->setDocReferNum($o_SoDeliveryBN->getDocReferNum()); endif;
      if($o_SoDeliveryBN->getFacturaNum()) : $this->setFacturaNum($o_SoDeliveryBN->getFacturaNum()); endif;
      if($o_SoDeliveryBN->getMoneda()) : $this->setMoneda($o_SoDeliveryBN->getMoneda()); endif;
      if($o_SoDeliveryBN->getFechaRequerida()) : $this->setFechaRequerida($o_SoDeliveryBN->getFechaRequerida()); endif;
      if($o_SoDeliveryBN->getFechaProgramada()) : $this->setFechaProgramada($o_SoDeliveryBN->getFechaProgramada()); endif;
      if($o_SoDeliveryBN->getFechaSalida()) : $this->setFechaSalida($o_SoDeliveryBN->getFechaSalida()); endif;
      if($o_SoDeliveryBN->getFlete()) : $this->setFlete($o_SoDeliveryBN->getFlete()); endif;
      if($o_SoDeliveryBN->getTotalBruto()) : $this->setTotalBruto($o_SoDeliveryBN->getTotalBruto()); endif;
      if($o_SoDeliveryBN->getNeto()) : $this->setNeto($o_SoDeliveryBN->getNeto()); endif;
      if($o_SoDeliveryBN->getObs()) : $this->setObs($o_SoDeliveryBN->getObs()); endif;
      if($o_SoDeliveryBN->getStatus()) : $this->setStatus($o_SoDeliveryBN->getStatus()); endif;
      if($o_SoDeliveryBN->getCreaUsr()) : $this->setCreaUsr($o_SoDeliveryBN->getCreaUsr()); endif;
      if($o_SoDeliveryBN->getCreaFecha()) : $this->setCreaFecha($o_SoDeliveryBN->getCreaFecha()); endif;
      if($o_SoDeliveryBN->getModifUsr()) : $this->setModifUsr($o_SoDeliveryBN->getModifUsr()); endif;
      if($o_SoDeliveryBN->getModifFecha()) : $this->setModifFecha($o_SoDeliveryBN->getModifFecha()); endif;
    }
  }

  /* Obtiene el n�mero de registros afectados */
  public function getRowsAffected()
  {
    return $this->nRowsAffected;
  }

  public function save()
  {
    try
    {
      $this->setId($this->getSecuencia());
      if($this->getId()){
        $a_Fields = array();

        if($this->getId()) : $a_Fields['dlv_id'] = ($this->getId()); endif;
        if($this->getTipo()) : $a_Fields['dlv_tipo'] = ($this->getTipo()); endif;
        if($this->getDlvNum()) : $a_Fields['dlv_dlvNum'] = ($this->getDlvNum()); endif;
        if($this->getAlmId()) : $a_Fields['dlv_almId'] = ($this->getAlmId()); endif;
        if($this->getClienteId()) : $a_Fields['dlv_clienteId'] = ($this->getClienteId()); endif;
        if($this->getClienteNombre()) : $a_Fields['dlv_clienteNombre'] = ($this->getClienteNombre()); endif;
        if($this->getContacto()) : $a_Fields['dlv_contacto'] = ($this->getContacto()); endif;
        if($this->getDocReferNum()) : $a_Fields['dlv_docReferNum'] = ($this->getDocReferNum()); endif;
        if($this->getFacturaNum()) : $a_Fields['dlv_facturaNum'] = ($this->getFacturaNum()); endif;
        if($this->getMoneda()) : $a_Fields['dlv_moneda'] = ($this->getMoneda()); endif;
        if($this->getFechaRequerida()) : $a_Fields['dlv_fechaRequerida'] = ($this->getFechaRequerida()); endif;
        if($this->getFechaProgramada()) : $a_Fields['dlv_fechaProgramada'] = ($this->getFechaProgramada()); endif;
        if($this->getFechaSalida()) : $a_Fields['dlv_fechaSalida'] = ($this->getFechaSalida()); endif;
        if($this->getFlete()) : $a_Fields['dlv_flete'] = ($this->getFlete()); endif;
        if($this->getTotalBruto()) : $a_Fields['dlv_totalBruto'] = ($this->getTotalBruto()); endif;
        if($this->getNeto()) : $a_Fields['dlv_neto'] = ($this->getNeto()); endif;
        if($this->getObs()) : $a_Fields['dlv_obs'] = ($this->getObs()); endif;
        if($this->getStatus()) : $a_Fields['dlv_status'] = ($this->getStatus()); endif;
        if($this->getCreaUsr()) : $a_Fields['dlv_creaUsr'] = ($this->getCreaUsr()); endif;
        if($this->getCreaFecha()) : $a_Fields['dlv_creaFecha'] = ($this->getCreaFecha()); endif;
        if($this->getModifUsr()) : $a_Fields['dlv_modifUsr'] = ($this->getModifUsr()); endif;
        if($this->getModifFecha()) : $a_Fields['dlv_modifFecha'] = ($this->getModifFecha()); endif;

        $sStringField = arrayClass::implode_keys(',', $a_Fields);
        $sStringValue = "'".implode("','", $a_Fields)."'";

        $sql = "INSERT INTO so_delivery (".$sStringField.") VALUES (".$sStringValue.");";
        $result = mysql_query($sql);
        if($result)
        {
          $this->nRowsAffected = (int) mysql_affected_rows();
        }
        else
        {
          throw new Exception( mysql_error() );
        }
      }
      else
      {
        throw new Exception( 'El id no esta registrado.');
      }
    }
    catch ( Exception $o_Error )
    {
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
    return true;
  }

  public function update()
  {
    try
    {
      if($this->getId())
      {
        $a_Fields = array();

        if($this->getId()) : $a_Fields['dlv_id'] = ($this->getId()); endif;
        if($this->getTipo()) : $a_Fields['dlv_tipo'] = ($this->getTipo()); endif;
        if($this->getDlvNum()) : $a_Fields['dlv_dlvNum'] = ($this->getDlvNum()); endif;
        if($this->getAlmId()) : $a_Fields['dlv_almId'] = ($this->getAlmId()); endif;
        if($this->getClienteId()) : $a_Fields['dlv_clienteId'] = ($this->getClienteId()); endif;
        if($this->getClienteNombre()) : $a_Fields['dlv_clienteNombre'] = ($this->getClienteNombre()); endif;
        if($this->getContacto()) : $a_Fields['dlv_contacto'] = ($this->getContacto()); endif;
        if($this->getDocReferNum()) : $a_Fields['dlv_docReferNum'] = ($this->getDocReferNum()); endif;
        if($this->getFacturaNum()) : $a_Fields['dlv_facturaNum'] = ($this->getFacturaNum()); endif;
        if($this->getMoneda()) : $a_Fields['dlv_moneda'] = ($this->getMoneda()); endif;
        if($this->getFechaRequerida()) : $a_Fields['dlv_fechaRequerida'] = ($this->getFechaRequerida()); endif;
        if($this->getFechaProgramada()) : $a_Fields['dlv_fechaProgramada'] = ($this->getFechaProgramada()); endif;
        if($this->getFechaSalida()) : $a_Fields['dlv_fechaSalida'] = ($this->getFechaSalida()); endif;
        if($this->getFlete()) : $a_Fields['dlv_flete'] = ($this->getFlete()); endif;
        if($this->getTotalBruto()) : $a_Fields['dlv_totalBruto'] = ($this->getTotalBruto()); endif;
        if($this->getNeto()) : $a_Fields['dlv_neto'] = ($this->getNeto()); endif;
        if($this->getObs()) : $a_Fields['dlv_obs'] = ($this->getObs()); endif;
        if($this->getStatus()) : $a_Fields['dlv_status'] = ($this->getStatus()); endif;
        if($this->getCreaUsr()) : $a_Fields['dlv_creaUsr'] = ($this->getCreaUsr()); endif;
        if($this->getCreaFecha()) : $a_Fields['dlv_creaFecha'] = ($this->getCreaFecha()); endif;
        if($this->getModifUsr()) : $a_Fields['dlv_modifUsr'] = ($this->getModifUsr()); endif;
        if($this->getModifFecha()) : $a_Fields['dlv_modifFecha'] = ($this->getModifFecha()); endif;

        $sStringField = arrayClass::implode_key_value(',', $a_Fields);

        $o_conexion = new BaseDatos();
        $con = $o_conexion->conectar();
        $sql = "UPDATE so_delivery SET ".$sStringField." WHERE 1 AND dlv_id='".$this->getId()."';";
        $result = mysql_query($sql);
        if($result)
        {
          $this->nRowsAffected = (int) mysql_affected_rows();
        }
        else
        {
          throw new Exception( mysql_error() );
        }
      }
      else
      {
        throw new Exception( 'El id no esta registrado.');
      }
    }
    catch ( Exception $o_Error )
    {
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
    return true;
  }

  public function delete()
  {
    try
    {
      if($this->getId())
      {
        $a_Fields = array();

        $o_conexion = new BaseDatos();
        $con = $o_conexion->conectar();
        $sql = "DELETE FROM so_delivery WHERE 1 AND dlv_id='".$this->getId()."';"; 
        $result = mysql_query($sql);
        if($result)
        {
          $this->nRowsAffected = (int) mysql_affected_rows($oLink);
        }
        else
        {
          throw new Exception( mysql_error($oLink) );
        }
      }
      else
      {
        throw new Exception( 'El id no esta registrado.');
      }
    }
    catch ( Exception $o_Error )
    {
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
    return true;
  }

  /* B�squeda del registro por la llave primaria */
  public static function selectByPK($vId)
  {
    try
    {
      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      $sql = "SELECT dlv_id, dlv_tipo, dlv_dlvNum, dlv_almId, dlv_clienteId, dlv_clienteNombre, dlv_contacto, dlv_docReferNum, dlv_facturaNum, dlv_moneda, dlv_fechaRequerida, dlv_fechaProgramada, dlv_fechaSalida, dlv_flete, dlv_totalBruto, dlv_neto, dlv_obs, dlv_status, dlv_creaUsr, dlv_creaFecha, dlv_modifUsr, dlv_modifFecha FROM so_delivery WHERE 1 AND dlv_id='".$vId."'; ";
        $result = mysql_query($sql);
      $o_Propel = $result ? mysql_fetch_object($result) : null;
      
      return $o_Propel?self::insertObjectBN($o_Propel):null;

    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* Devuelve un objeto con m�todos de acceso */
  private function insertObjectBN($o_propel){
    $o_SoDeliveryDP = new SoDeliveryBN();
    $o_SoDeliveryDP->setId($o_propel->dlv_id);
    $o_SoDeliveryDP->setTipo($o_propel->dlv_tipo);
    $o_SoDeliveryDP->setDlvNum($o_propel->dlv_dlvNum);
    $o_SoDeliveryDP->setAlmId($o_propel->dlv_almId);
    $o_SoDeliveryDP->setClienteId($o_propel->dlv_clienteId);
    $o_SoDeliveryDP->setClienteNombre($o_propel->dlv_clienteNombre);
    $o_SoDeliveryDP->setContacto($o_propel->dlv_contacto);
    $o_SoDeliveryDP->setDocReferNum($o_propel->dlv_docReferNum);
    $o_SoDeliveryDP->setFacturaNum($o_propel->dlv_facturaNum);
    $o_SoDeliveryDP->setMoneda($o_propel->dlv_moneda);
    $o_SoDeliveryDP->setFechaRequerida($o_propel->dlv_fechaRequerida);
    $o_SoDeliveryDP->setFechaProgramada($o_propel->dlv_fechaProgramada);
    $o_SoDeliveryDP->setFechaSalida($o_propel->dlv_fechaSalida);
    $o_SoDeliveryDP->setFlete($o_propel->dlv_flete);
    $o_SoDeliveryDP->setTotalBruto($o_propel->dlv_totalBruto);
    $o_SoDeliveryDP->setNeto($o_propel->dlv_neto);
    $o_SoDeliveryDP->setObs($o_propel->dlv_obs);
    $o_SoDeliveryDP->setStatus($o_propel->dlv_status);
    $o_SoDeliveryDP->setCreaUsr($o_propel->dlv_creaUsr);
    $o_SoDeliveryDP->setCreaFecha($o_propel->dlv_creaFecha);
    $o_SoDeliveryDP->setModifUsr($o_propel->dlv_modifUsr);
    $o_SoDeliveryDP->setModifFecha($o_propel->dlv_modifFecha);
    return $o_SoDeliveryDP;
  }
}